package cn.autoOffice.controller;

import cn.autoOffice.bean.pojo.Employee;
import cn.autoOffice.service.EmpService;
import cn.autoOffice.service.RedisService;
import cn.office.shiro.vo.Result;
import cn.office.shiro.vo.ResultCodeEnum;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 金健伟
 * @DATE 2023/3/27 11:04
 * 功能说明：
 */
@RestController
@RequestMapping("/employee")
public class EmpController {

    @Autowired
    private EmpService empService;

    @Autowired
    private RedisService redisService;

    // （财务部门）查询员工通讯录信息
    @GetMapping("/selectEmpInfo")
    // @RequiresRoles("financeUser")
    public Result<PageInfo<Employee>> getEmployeeInfoByCode(
            @RequestParam(value = "empCode", required = false) String empCode,
            @RequestParam("currentPage") Integer currentPage,
            @RequestParam("pageSize") Integer pageSize) {

        PageInfo<Employee> pageInfo;
        List<Employee> employeeList;
        Integer countEmp = empService.countEmp();
        String empCount = redisService.getValue("allEmp" + currentPage);
        if (empCount != null){
            employeeList = JSONObject.parseArray(empCount,Employee.class);
            pageInfo = new PageInfo(employeeList);
            pageInfo.setTotal(countEmp);
        } else {
            PageHelper.startPage(currentPage,pageSize);
            employeeList = empService.getEmployeeInfoByCode(empCode);
            pageInfo = new PageInfo<>(employeeList);
            redisService.putValue("allEmp" + currentPage, employeeList, 30);
            redisService.putValue("allEmpCount",pageInfo.getTotal(),30);
        }
        return new Result<>(ResultCodeEnum.SUCCESS, pageInfo);
    }

    @GetMapping("/addEmployee")
    // @RequiresRoles("financeUser")
    public Result<Integer> addEmployee(Employee employee) {
        int res = empService.addEmployee(employee);
        return new Result<>(res > 0 ?
                ResultCodeEnum.SUCCESS :
                ResultCodeEnum.FAIL, res);
    }

    @GetMapping("/updateEmployee")
    // @RequiresRoles("financeUser")
    public Result<Integer> updateEmployee(Employee employee) {
        int res = empService.updateEmployee(employee);
        return new Result<>(res > 0 ?
                ResultCodeEnum.SUCCESS :
                ResultCodeEnum.FAIL, res);
    }

    @GetMapping("/deleteEmployee")
    // @RequiresRoles("financeUser")
    public Result<Integer> deleteEmployee(int id) {
        int res = empService.deleteEmployee(id);
        return new Result<>(res > 0 ?
                ResultCodeEnum.SUCCESS :
                ResultCodeEnum.FAIL, res);
    }

    @GetMapping("/logout")
    public Result<?> logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new Result<>(ResultCodeEnum.SUCCESS);
    }

}
